﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Data;

using agvcontroller.Common;
using agvcsimulator.Driver;
using agvcontroller.Log;
using agvcontroller.Common.DataBase;

namespace agvcsimulator.Module
{
    public class CTerminalSim
    {
        public int ID
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public bool TerminalUse
        {
            get;
            set;
        }

        public int TerminalNodeID
        {
            get;
            set;
        }

        public IPAddress TerminalIP
        {
            get;
            set;
        }

        public IPAddress AGVCIP
        {
            get;
            set;
        }

        public int Port
        {
            get;
            set;
        }

        List<bool> RequestJobList = new List<bool>();
        List<bool> RequestJobLampList = new List<bool>();
        List<bool> CancelJobList = new List<bool>();
        public List<int> JobNode = new List<int>();

        public CTerminalSimSocketDriver SimDriver;

        Logger Log;

        public CTerminalSim(string name)
        {
            Log = new Logger(name);

            this.Name = name;
        }

        public int InitModule()
        {
            SimDriver = new CTerminalSimSocketDriver(AGVCIP, Port);

            return CReturnCode.SUCCESS;
        }

        void RegisterEvent()
        {
            SimDriver.onConnect += new deleTerminalSocketSimOnConnect(SimDriver_onConnect);
            SimDriver.OnRcvMsg += new deleTerminalRCVMsg(SimDriver_OnRcvMsg);
            SimDriver.OnReqIO += new deleTerminalRequestIO(SimDriver_OnReqIO);
        }

        void SimDriver_OnReqIO(object sender)
        {
            throw new NotImplementedException();
        }

        void SimDriver_OnRcvMsg(object sender, string msg)
        {
            throw new NotImplementedException();
        }

        void SimDriver_onConnect(object sender, enumConnect connect)
        {
        }

        public int Connect()
        {
            return SimDriver.Connect();
        }

        public int Disconnect()
        {
            return SimDriver.Close();
        }

        public int RequestJob(int nodeNo)
        {
            SimDriver.Send(string.Format("REQUESTJOB:{0}", nodeNo));
            return CReturnCode.SUCCESS;
        }

        public int CancelJob(int nodeNo)
        {
            SimDriver.Send(string.Format("CANCELJOB:{0}", nodeNo));
            return CReturnCode.SUCCESS;
        }
    }

    public class CTerminalSimDic : Dictionary<int, CTerminalSim>
    {
        CAGVCDB AGVCDB;
        Logger Log;

        private static CTerminalSimDic uniqueInstance;

        public static CTerminalSimDic getInstance()
        {
            if (uniqueInstance == null)
            {
                uniqueInstance = new CTerminalSimDic();
            }
            return uniqueInstance;
        }

        public CTerminalSimDic()
        {
            Log = new Logger("CVehicleSim");
            AGVCDB = CAGVCDB.getInstance();

            GetDataFromDB();
            InitModule();
        }

        public int InitModule()
        {
            foreach (KeyValuePair<int, CTerminalSim> terminalSim in this)
            {
                terminalSim.Value.InitModule();
            }
            return CReturnCode.SUCCESS;
        }

        public int GetDataFromDB()
        {
            try
            {
                DataTable table = AGVCDB.AGVCDataSet.Tables["TerminalList"];
                CTerminalSim terminalSim;

                foreach (DataRow row in table.Rows)
                {
                    terminalSim = new CTerminalSim(Convert.ToString(row["Name"]));
                    terminalSim.ID = Convert.ToInt32(row["ID"]);
                    terminalSim.AGVCIP = IPAddress.Parse(Convert.ToString(row["AGVCIP"]));
                    terminalSim.TerminalIP = IPAddress.Parse(Convert.ToString(row["TerminalIP"]));
                    terminalSim.Port = Convert.ToInt32(row["Port"]);
                    terminalSim.TerminalNodeID = Convert.ToInt32(row["NodeID"]);

                    for (int i = 0; i < 10; i++)
                    {
                        terminalSim.JobNode.Add(Convert.ToInt32(row[string.Format("Job{0}_NodeID", i + 1)]));
                    }

                    terminalSim.TerminalUse = Convert.ToBoolean(row["TerminalUse"]);

                    this.Add(terminalSim.ID, terminalSim);
                }
            }
            catch (Exception ex)
            {
                Log.Exception(string.Format("{0},{1}", ex.Message, ex.StackTrace));
                return CReturnCode.ERR_DATA_ACQUIRING_FAIL;
            }
            return CReturnCode.SUCCESS;

        }
    }
}
